package com.legendary.awesome;

import com.legendary.awesome.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;


public class ChangeDate extends Activity {
	
	private Button changedate_ok = null;
	private Intent recieved; //intent som blir tatt i mot
	private DatePicker dp =  null;
	
	private String VISIBILITY = "GONE";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.change_date);
		
		recieved  = getIntent();
		
		if(recieved == null)
		{
			return;
		}
		
		initComponents();
	}
	
	public void initComponents()
	{
		//Button
		changedate_ok = (Button) findViewById(R.id.bchange_date_ok);
		
		//DatePicker
		dp = (DatePicker) findViewById(R.id.dp_id);
		
		//Add Listener
		changedate_ok.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				if(changedate_ok.isPressed())
				{
					finish();
				}
			}
			
		});
	}
	
	public void sendDataViaIntent(Intent intent)
	{
		intent.putExtra("YEAR", dp.getYear());
		intent.putExtra("MONTH", dp.getMonth()+1);
		intent.putExtra("DAY", dp.getDayOfMonth());
		intent.putExtra("VISIBILITY", VISIBILITY);
		
		setResult(RESULT_OK, intent);
	}
	
	@Override
	protected void onPause()
	{
		super.onPause();
		finish();
	}
	
	@Override
	public void finish()
	{
		if(recieved != null)
		{
			sendDataViaIntent(recieved);
		}
		super.finish();
	}
	
	//Getters and Setters
	public Button getChangedate_ok() {
		return changedate_ok;
	}

	public void setChangedate_ok(Button changedate_ok) {
		this.changedate_ok = changedate_ok;
	}
}
